<?php
/**
 * 楼盘基本信息上传
 */
namespace app\manage\controller;
use think\Controller;
use think\Model;
use think\paginator\driver\Bootstrap;
header("Content-Type: text/html; charset=UTF-8");
class Index extends \app\common\controller\Manage
{
    //主页页面
    public function index()
    {
        $data = input('param.');
        $this -> assign('data',$data);
        $result = $this -> findBuilding($data);
        $new = end(array_column($result,'id'));
        if($result !== ''){
            $curpage = input('page') ? input('page') : 1;//当前第x页，有效值为：1,2,3,4,5...
            $listRow = 8;//每页2行记录
            $showdata = array_chunk($result, $listRow, true)[$curpage-1];
            $p = Bootstrap::make($showdata, $listRow, $curpage, count($result), false, [
                'var_page' => 'page',
                'path'     => url("/manage/index/index"),//这里根据需要修改url
                'query'    => [],
                'fragment' => '',
            ]);
            $p ->appends($_GET);
            $this -> assign('info',$p);
            $this -> assign('num',$new);
            $this -> assign('page',$p->render());
        }else{
            $result = '';
        }
        $number = $this -> buildingNum($data);
        $this -> assign('count',$number);
        return view();
    }
    //添加楼盘信息页
    public function inserts(){
        $data = input('param.');
        if(isset($data['id'])){
            //查询到当前的图文信息
            $info1 = \app\common\model\Building::with('buildingDesc') -> Where('id',$data['id']) -> find();
            if($info1){
                $info = $info1 -> toArray();
            }
            if($info){
                if($info['building_desc']){
                    $new = [];
                    $type = explode(',',$info['type']);
                    foreach ($type as $item){
                        foreach ($info['building_desc'] as $more){
                            if($more['id'] == $item){
                                $new[] = $more;
                                $num = $item;
                            }
                        }
                    }
                    $arr = array('num' => $num);
                    $array = array('building_desc2' => $new);
                    $info2 = array_merge($info,$array,$arr);
                    unset($info2['building_desc']);
                }else{
                    $array = array('building_desc2' => '');
                    $info2 = array_merge($info,$array);
                    unset($info2['building_desc']);
                }
                }
            }
            if(!isset($info2)){
                $info2 = '';
            }
            //将img字符串解开成为数组
            foreach ($info2['building_desc2'] as $k => $value){
                if($value['img']){
                    $va = strpos($value['img'],',');
                    if($va == false){
                        $info2['building_desc2'][$k]['img'] = array($value['img']);
                    }else{
                        $arr = explode(',',$value['img']);
                        $info2['building_desc2'][$k]['img'] = $arr;
                    }
                }
            }
            $this -> assign('info',$info2);
            return view();
    }
    //通过传入数据区分楼盘信息
    protected function findBuilding($data){
        if ($data['key'])
            $findMap[] = ['name','like',"%$data[key]%"];

        if ($data['mark'] =='qb'){
            //展示公司楼盘
            switch ($data['company']){
                case 'fj':
                    $findMap[] = ['company','eq','房金时代网络科技有限公司'];
                    break;
                case 'lz':
                    $findMap[] = ['company','eq','罗宅网络科技有限公司'];
                    break;
                case 'th':
                    $findMap[] = ['company','eq','领郡置业有限公司'];
                    break;
                case 'hc':
                    $findMap[] = ['company','neq',''];break;
                    default;
            }
        }else{
            //展示分类
            switch ($data['mark']){
                case 'yx':
                    $findMap[] = ['list','eq','1'];
                    $list = 1;
                    break;
                case 'gd':
                    $findMap[] = ['list','eq','2'];
                    $list = 2;
                    break;
                case 'sy':
                    $findMap[] = ['list','eq','3'];
                    $list = 3;
                    break;
                case 'hw':
                    $findMap[] = ['list','eq','4'];
                    $list = 4;
                    break;
                case 'wl':
                    $findMap[] = ['list','eq','5'];
                    $list = 5;
                    break;
                case 'gn':
                    $findMap[] = ['list_enterlo','eq',1];
                    $list = 1;
                    break;
                case 'gw':
                    $findMap[] = ['list_enterlo','eq',2];
                    $list = 2;
                    break;default;
            }
        }

        $info = \app\common\model\Building::where($findMap)->select();
        if ($info)
            $info = $info ->toArray();
        //全部楼盘排序
        if ($data['mark'] =='qb'){
            usort($info,function ($a,$b){
                if ($a['date'] == $b['date'])
                    return 0;
                return ($a['date'] < $b['date']) ?-1 :1;
            });
            return $info;
        }
        else{
            if(in_array($data['mark'],array('gn','gw'))){
                $orderInfo = \app\common\model\BuildingListEnterlo::where('list',$list)->find();
            }else{
                $orderInfo = \app\common\model\BuildingList::where('list',$list)->find();
            }
            $orderInfoArr = explode(',',$orderInfo['building_id']);
            $num = end($orderInfoArr);
            $infoPre = [];
            foreach ($orderInfoArr as $value){
                foreach ($info as$k=> $item){
                    if ($value == $item['id']){
                        $infoPre[] = $item;
                        unset($info[$k]);
                        break;
                    }
                }
            }
            return $infoPre;
        }
    }
    //每个公司的楼盘数目
    private function buildingNum(){
        $map1[] = ['company','eq','房金时代网络科技有限公司'];
        $info1 = \app\common\model\Building::where($map1) -> select();
        $count1 = count($info1);
        $map2[] = ['list','eq',1];
        $info2 = \app\common\model\Building::where($map2) -> select();
        $count2 = count($info2);
        $map3[] = ['list','eq',2];
        $info3 = \app\common\model\Building::where($map3) -> select();
        $count3 = count($info3);
        $map4[] = ['list','eq',3];
        $info4 = \app\common\model\Building::where($map4) -> select();
        $count4 = count($info4);
        $number = array($count1,$count2,$count3,$count4);

        $map11[] = ['company','eq','罗宅网络科技有限公司'];
        $info11 = \app\common\model\Building::where($map11) -> select();
        $count11 = count($info11);
        $map22[] = ['list','eq',4];
        $info22 = \app\common\model\Building::where($map22) -> select();
        $count22 = count($info22);
        $map33[] = ['list','eq',5];
        $info33 = \app\common\model\Building::where($map33) -> select();
        $count33 = count($info33);
        $number1 = array($count11,$count22,$count33);

        $map12[] = ['company','eq','领郡置业有限公司'];
        $info12 = \app\common\model\Building::where($map12) -> select();
        $count12 = count($info12);
        $number2 = array($count12);

        $all = \app\common\model\Building::count();
        $mapgn[] = ['list_enterlo','eq',1];
        $mapgw[] = ['list_enterlo','eq',2];
        $gn = \app\common\model\Building::where($mapgn) -> count();
        $gw = \app\common\model\Building::where($mapgw) -> count();
        $number3 = array(intval($all),intval($gn),intval($gw));
        $number4 = array_merge($number,$number1,$number2,$number3);
        return $number4;
    }
    //测试代码
    public function ceshi(){
        $num = \app\common\model\Building::where('company','房金时代网络科技有限公司') -> column('id');
        foreach ($num as $va){
            $a = $a.','.$va;
        }
        return view();
        $data = input('param.');
        //将img字符串解开成为数组
        foreach ($info2['building_desc2'] as $k => $value){
            $va = strpos($value['img'],',');
            if($va == false){
                $info2['building_desc2'][$k]['img'] = array($value['img']);
            }else{
                $arr = explode(',',$value['img']);
                $info2['building_desc2'][$k]['img'] = $arr;
            }
        }
        $a = '1,2,3,4';
        $info = \app\common\model\Building::where('id',1) -> setField('type',$a);
        exit;
        $aa = array(1,2,3,4);
        $res = '';
        foreach ($aa as $value){
            if(!$res){
                $res = $value;
            }else{
                $res = $res.','.$value;
            }
        }
        dump($res);exit;
        $info = \app\common\model\Building::with('buildingDesc')->select()->toArray();
        $new = [];
        foreach ($info as $value){
            $type = explode(',',$value['type']);
            foreach ($type as $item){
                foreach ($value['building_desc'] as $more){
                    if($more['id'] ==$item){
                        $new[] = $more;
                    }
                }
            }
        }
        dump($new);exit;
        $this->assign('index',$new);
        return $this -> fetch();
    }

}